//错误码
//通用错误
/* eslint-disable */
var ERROR_CODE_UNKNOWN = 1000; //未知错误
var ERROR_CODE_NETWORKERROR = 1001; //网络错误
var ERROR_CODE_PARAMERROR = 1002; //缺少插件元素

//登录模块
var ERROR_CODE_LOGIN_NOLOGIN = 2000; // 未登录
var ERROR_CODE_LOGIN_REPEATLOGIN = 2001; //设备已登录，重复登录
var ERROR_CODE_LOGIN_NOSUPPORT = 2002; //当前设备不支持Digest登录

//预览播放
var ERROR_CODE_PLAY_PLUGININITFAIL = 3000; //插件初始化失败
var ERROR_CODE_PLAY_NOREPEATPLAY = 3001; //当前窗口已经在预览
var ERROR_CODE_PLAY_PLAYBACKABNORMAL = 3002; //回放异常
var ERROR_CODE_PLAY_PLAYBACKSTOP = 3003; //回放停止
var ERROR_CODE_PLAY_NOFREESPACE = 3004; //录像过程中，硬盘容量不足

var version = "V3.3.0build20230322";

//对讲
var ERROR_CODE_TALK_FAIL = 5000; //语音对讲失败
// 初始化插件

function WebVideo() {
  this.g_iWndIndex = 0;
  this.szDeviceIdentify = "";
  this.deviceport = "";
  this.rtspPort = "";
  this.channels = [];
  this.ip = "";
  this.port = "80";
  this.username = "";
  this.password = "";
  this.init = function (ip, username, password) {
    this.ip = ip;
    this.username = username;
    this.password = password;
    // var self = this
    // 初始化插件参数及插入插件
    WebVideoCtrl.I_InitPlugin({
      bWndFull: true,
      // iPackageType: 2,
      iWndowType: 1, //分屏
      cbInitPluginComplete: function () {
        WebVideoCtrl.I_InsertOBJECTPlugin("divPlugin").then(
          () => {
            // 检查插件是否最新
            WebVideoCtrl.I_CheckPluginVersion().then((bFlag) => {
              console.log(bFlag);
              if (bFlag) {
                alert(
                  "检测到新的插件版本，双击开发包目录里的HCWebSDKPlugin.exe升级！"
                );
              }
            });
          },
          () => {
            alert(
              "插件初始化失败，请确认是否已安装插件；如果未安装，请双击开发包目录里的HCWebSDKPlugin.exe安装！"
            );
          }
        );
        $(window).bind({
          resize: function () {
            //WebVideoCtrl.I_Resize($("body").width(), $("body").height());
          },
        });
      },
    });
  };
  // 登录
  this.clickLogin = function () {
    var self = this;
    if ("" == self.ip || "" == self.port) {
      return;
    }
    self.szDeviceIdentify = self.ip + "_" + self.port;
    console.log(self.szDeviceIdentify);
    WebVideoCtrl.I_Login(self.ip, 1, self.port, self.username, self.password, {
      success: function (xmlDoc) {
        console.log(xmlDoc);
        setTimeout(function () {
          console.log("登录成功");
          self.getChannelInfo();
          self.getDevicePort();
        }, 10);
        setTimeout(function () {
          self.clickStartRealPlay();
        }, 500);
      },
      error: function (oError) {
        console.log("oError");
      },
    });
  };
  // 退出
  this.clickLogout = function () {
    var self = this;
    self.channels = [];

    if (null == self.szDeviceIdentify) {
      return;
    }
    var iRet = WebVideoCtrl.I_Logout(self.szDeviceIdentify);
    if (0 == iRet) {
      self.getChannelInfo();
      self.getDevicePort();
    }
  };
  // 获取通道
  this.getChannelInfo = function () {
    var self = this;
    self.channels = [];
    if (null == self.szDeviceIdentify) {
      return;
    }
    // 模拟通道
    WebVideoCtrl.I_GetAnalogChannelInfo(self.szDeviceIdentify, {
      async: false,
      success: function (xmlDoc) {
        var oChannels = $(xmlDoc).find("VideoInputChannel");
        $.each(oChannels, function (i) {
          var id = $(this).find("id").eq(0).text(),
            name = $(this).find("name").eq(0).text();
          if ("" == name) {
            name = "Camera " + (i < 9 ? "0" + (i + 1) : i + 1);
          }
          self.channels.push({
            id: id,
            name: name,
          });
        });
        console.log("获取模拟通道号成功");
      },
      error: function (status, xmlDoc) {
        console.log("获取模拟通道号失败");
      },
    });
  };
  // 获取端口
  this.getDevicePort = function () {
    var self = this;
    if (null == self.szDeviceIdentify) {
      return;
    }
    var oPort = WebVideoCtrl.I_GetDevicePort(self.szDeviceIdentify);
    if (oPort != null) {
      self.deviceport = oPort.iDevicePort;
      self.rtspPort = oPort.iRtspPort;
    }
    console.log("获取端口号成功");
  };

  // 开始预览
  this.clickStartRealPlay = function () {
    var self = this;
    var oWndInfo = WebVideoCtrl.I_GetWindowStatus(self.g_iWndIndex),
      iChannelID = self.channels[0].id;

    if (null == self.szDeviceIdentify) {
      return;
    }
    var startRealPlay = function () {
      console.log(self.szDeviceIdentify);
      WebVideoCtrl.I_StartRealPlay(self.szDeviceIdentify, {
        iChannelID: iChannelID,
        bZeroChannel: false,
        iStreamType: 2,
        success: function () {
          console.log("预览成功");
        },
        error: function (status, xmlDoc) {
          if (403 === status) {
            console.log("设备不支持Websocket取流");
          } else {
            console.log("预览失败");
          }
        },
      });
    };

    if (oWndInfo != null) {
      // 已经在播放了，先停止
      WebVideoCtrl.I_Stop({
        success: function () {
          startRealPlay();
        },
      });
    } else {
      startRealPlay();
    }
  };
  // 停止预览
  this.clickStopRealPlay = function () {
    var self = this;
    var oWndInfo = WebVideoCtrl.I_GetWindowStatus(self.g_iWndIndex);
    if (oWndInfo != null) {
      WebVideoCtrl.I_Stop({
        success: function () {
          console.log("停止预览成功");
        },
        error: function () {
          console.log("停止预览失败");
        },
      });
    }
  };
  this.mouseDownPTZControl = function (iPTZIndex) {
    var oWndInfo = WebVideoCtrl.I_GetWindowStatus(self.g_iWndIndex),
      iPTZSpeed = 4;

    if (oWndInfo != null) {
      if (9 == iPTZIndex) {
        iPTZSpeed = 0;
      }
      WebVideoCtrl.I_PTZControl(iPTZIndex, false, {
        iPTZSpeed: iPTZSpeed,
        success: function (xmlDoc) {
          console.log("开启成功");
        },
        error: function (status, xmlDoc) {
          console.log("开启失败");
        },
      });
    }
  };
  this.mouseUpPTZControl = function () {
    var oWndInfo = WebVideoCtrl.I_GetWindowStatus(self.g_iWndIndex);

    if (oWndInfo != null) {
      WebVideoCtrl.I_PTZControl(1, true, {
        success: function (xmlDoc) {
          console.log(" 停止云台成功！");
        },
        error: function (status, xmlDoc) {
          console.log(" 停止云台失败！", status, xmlDoc);
        },
      });
    }
  };
  this.PTZZoomStop = function () {
    var oWndInfo = WebVideoCtrl.I_GetWindowStatus(self.g_iWndIndex);

    if (oWndInfo != null) {
      WebVideoCtrl.I_PTZControl(11, true, {
        iWndIndex: self.g_iWndIndex,
        success: function (xmlDoc) {
          console.log(" stop zoom success!");
        },
        error: function (status, xmlDoc) {
          console.log(" stop zoom fail!");
        },
      });
    }
  };
  this.PTZZoomIn = function () {
    var oWndInfo = WebVideoCtrl.I_GetWindowStatus(self.g_iWndIndex);

    if (oWndInfo != null) {
      WebVideoCtrl.I_PTZControl(10, false, {
        iWndIndex: self.g_iWndIndex,
        success: function (xmlDoc) {
          console.log(" Zoom+success!", xmlDoc);
        },
        error: function (status, xmlDoc) {
          console.log(" Zoom+fail!");
        },
      });
    }
  };

  this.PTZZoomout = function () {
    var oWndInfo = WebVideoCtrl.I_GetWindowStatus(self.g_iWndIndex);

    if (oWndInfo != null) {
      WebVideoCtrl.I_PTZControl(11, false, {
        iWndIndex: self.g_iWndIndex,
        success: function (xmlDoc) {
          console.log(" Zoom-success!");
        },
        error: function (status, xmlDoc) {
          console.log("  Zoom-failed!");
        },
      });
    }
  };
}
export default WebVideo;
